import QtQuick 2.12
import QtQuick.Window 2.12
import QtQuick.VirtualKeyboard 2.4

Window {
    id: flw
    visible: inputPanel.active
    width: Math.min(Screen.height*16/15, Screen.width)
    height: width*3/8
    x: (Screen.width-width)/2
    y: Screen.height-height
    flags: Qt.BypassWindowManagerHint
    color: "#a1a1a1"
    property  int pan: 32
    property int movThreshold: 4
    minimumWidth: 512
    minimumHeight: 160+20

    function resize() {

        var newW = Math.ceil(inputPanel.width+pan*2)
        var newH = inputPanel.keyboard.wordCandidateView.visibleCondition
                   ? Math.ceil(inputPanel.height*33/25+pan):
                   Math.ceil(inputPanel.height+pan*2)
        x += (width-newW)/2
        y += height - newH
        width = newW
        height = newH

    }
    onWidthChanged: inputPanel.resize()
    onHeightChanged: inputPanel.resize()
    Component.onCompleted: resize()
    MouseArea {
        //id: top
        acceptedButtons: Qt.LeftButton
        cursorShape: Qt.SizeAllCursor
        property real oldX
        property real oldY
        onPressed: {
            oldX = mouseX;
            oldY = mouseY;
        }
        onPositionChanged: {
            if(pressedButtons === Qt.LeftButton) {
                var dx = mouseX - oldX
                var dy = mouseY - oldY
                if(Math.abs(dx)+Math.abs(dy) > flw.movThreshold) {
                    flw.x = flw.x + dx
                    flw.y = flw.y + dy
                }

            }

        }
        y: 0
        height: flw.height-inputPanel.height
        x: flw.pan
        width: flw.width-flw.pan*2

    }

    MouseArea {
        //id: topLeft
        acceptedButtons: Qt.LeftButton
        property real oldX
        property real oldY
        cursorShape: Qt.SizeFDiagCursor
        onPressed: {
            oldX = mouseX
            oldY = mouseY
        }
        onReleased: flw.resize()
        onPositionChanged: {
            if(pressedButtons === Qt.LeftButton) {
                var dx = mouseX - oldX
                var dy = mouseY - oldY
                if(Math.abs(dx)+Math.abs(dy)>flw.movThreshold) {
                    var newWidth = Math.max(flw.width-dx, flw.minimumWidth)
                    var newHeight = Math.max(flw.height-dy, flw.minimumHeight)
                    flw.x = flw.x + (flw.width-newWidth )
                    flw.y = flw.y + (flw.height-newHeight)
                    flw.width = newWidth
                    flw.height = newHeight

                }


            }

        }
        y: 0
        x: 0
        height: flw.pan
        width: flw.pan

    }
    MouseArea {
        //id: bottomLeft
        acceptedButtons: Qt.LeftButton
        property real oldX
        property real oldY
        cursorShape: Qt.SizeBDiagCursor
        onPressed: {
            oldX = mouseX
            oldY = mouseY
        }
        onReleased: flw.resize()

        onPositionChanged: {
            if(pressedButtons === Qt.LeftButton) {
                var dx = mouseX - oldX
                var dy = mouseY - oldY
                if(Math.abs(dx)+Math.abs(dy)>flw.movThreshold) {
                    var newWidth = Math.max(flw.width-dx, flw.minimumWidth)
                    flw.x = flw.x + (flw.width-newWidth )
                    flw.width = newWidth
                    flw.height =  Math.max(flw.height+dy, flw.minimumHeight)

                }
            }
        }
        height: flw.pan
        width: flw.pan
        y: flw.height-height
        x: 0


    }
    MouseArea {
        //id: topRight
        acceptedButtons: Qt.LeftButton
        property real oldX
        property real oldY
        cursorShape: Qt.SizeBDiagCursor
        onPressed: {
            oldX = mouseX
            oldY = mouseY
        }
        onReleased: flw.resize()
        onPositionChanged: {
            if(pressedButtons === Qt.LeftButton) {
                var dx = mouseX - oldX
                var dy = mouseY - oldY
                if(Math.abs(dx)+Math.abs(dy)>flw.movThreshold) {
                    flw.width = Math.max(flw.width+dx, flw.minimumWidth)
                    var newHeight = Math.max(flw.height-dy, flw.minimumHeight)
                    flw.y = flw.y + (flw.height-newHeight )
                    flw.height = newHeight

                }
            }
        }
        height: flw.pan
        width: flw.pan
        y: 0
        x: flw.width-width

    }
    MouseArea {
        //id: left
        //visible: false
        acceptedButtons: Qt.LeftButton
        property real oldX
        property real oldY
        cursorShape: Qt.SizeHorCursor
        onPressed: {
            oldX = mouseX;
            oldY = mouseY;
        }
        onReleased: flw.resize()

        onPositionChanged: {
            if(pressedButtons === Qt.LeftButton) {
                var dx = mouseX - oldX
                var dy = mouseY - oldY
                if(Math.abs(dx)+Math.abs(dy)>flw.movThreshold) {
                    var newWidth = Math.max(flw.width - dx, flw.minimumWidth)
                    flw.x = flw.x + (flw.width-newWidth)
                    flw.width = newWidth
                }

            }

        }
        y: flw.pan
        x: 0
        height: flw.height - flw.pan*2
        width: flw.pan

    }
    MouseArea {
        //id: right
        //visible: false
        acceptedButtons: Qt.LeftButton
        property real oldX
        property real oldY
        cursorShape: Qt.SizeHorCursor
        onPressed: {
            oldX = mouseX;
            oldY = mouseY;
        }
        onReleased: flw.resize()

        onPositionChanged: {
            if(pressedButtons === Qt.LeftButton) {
                var dx = mouseX - oldX
                var dy = mouseY - oldY
                if(Math.abs(dx)+Math.abs(dy)>flw.movThreshold) {
                    flw.width = Math.max(flw.width + dx, flw.minimumWidth)

                }

            }

        }
        height: flw.height - flw.pan*2
        width: flw.pan
        y: flw.pan
        x: flw.width-width


    }
    MouseArea {
        id: bottom
        //visible: false
        acceptedButtons: Qt.LeftButton
        property real oldX
        property real oldY
        cursorShape: Qt.SizeVerCursor
        onPressed: {
            oldX = mouseX;
            oldY = mouseY;
        }
        onReleased: flw.resize()
        onPositionChanged: {
            if(pressedButtons === Qt.LeftButton) {
                var dx = mouseX - oldX
                var dy = mouseY - oldY
                if(Math.abs(dx)+Math.abs(dy)>flw.movThreshold) {
                    flw.height = Math.max(flw.height + dy, flw.minimumHeight)
                }


            }

        }
        y: flw.height-height
        x: flw.pan
        height: flw.pan
        width: flw.width-2*flw.pan

    }
    MouseArea {
        id: bottomRight
        acceptedButtons: Qt.LeftButton
        property real oldX
        property real oldY
        cursorShape: Qt.SizeFDiagCursor
        onPressed: {
            oldX = mouseX;
            oldY = mouseY;

        }
        onReleased: flw.resize()
        onPositionChanged: {
            if(pressedButtons === Qt.LeftButton) {
                var dx = mouseX - oldX
                var dy = mouseY - oldY
                if(Math.abs(dx)+Math.abs(dy)>flw.movThreshold) {
                    var newWidth = Math.max(flw.width+dx, flw.minimumWidth)
                    var newHeight = Math.max(flw.height+dy, flw.minimumHeight)
                    flw.width = newWidth
                    flw.height = newHeight
                }

            }

        }
        height: flw.pan
        width: flw.pan
        y: flw.height-height
        x: flw.width-width
    }

    TextInput{
        objectName: "preEdit"
        id:preEdit
        x: inputPanel.x
        y: 0
        width: inputPanel.width
        height: inputPanel.height/10
        z: -1
        font.pixelSize: height*0.8
        onTextChanged: {
            if(preeditText === "") clear()
        }
    }





    Binding {
        target: inputPanel.keyboard.wordCandidateView
        property: "height"
        value: inputPanel.keyboard.height/5
    }

    InputPanel {
        id: inputPanel
        x: (flw.width-width)/2
        y: flw.height-height-flw.pan
        Component.onCompleted: {
            keyboard.style.keyboardRelativeLeftMargin = 0
            keyboard.style.keyboardRelativeRightMargin = 0
            keyboard.style.keyboardRelativeBottomMargin = 0
        }
        function resize() {
            width = keyboard.wordCandidateView.visibleCondition
                    ? Math.min((flw.height-flw.pan)*80/33, flw.width-flw.pan*2) :
                      Math.min((flw.height-flw.pan*2)*16/5, flw.width-flw.pan*2)
        }



    }

    Connections {
        target: inputPanel.keyboard.wordCandidateView
        onVisibleConditionChanged: flw.resize()
    }

}
